package com.baidu.day06.Goods;

import java.util.ArrayList;

public class UserService {

    static ArrayList<User> userList = new ArrayList<>();
    static {
        userList.add(new User(1, "admin", "admin", 0.0));
        userList.add(new User(2, "test", "test", 1000.0));
    }

    public boolean register(User user) {
        for (User u : userList) {
            if (u.getAccount().equals(user.getAccount())) {
                System.out.println("该账号已经存在");
                  return false;
            }
        }
        return userList.add(user);
    }

    public User login(String account, String password) {
        for (User u : userList) {
            if (u.getAccount().equals(account) && u.getPassword().equals(password)) {
                return u;
            }
        }
        return null;
    }
    //查看所有用户信息
    public void findAllUser() {
        for (User u : userList) {
            System.out.println("用户帐号: "+u.getAccount()+"余额: "+u.getBalance());
        }
    }
    //购买
    public void buy(String goodsName, int number) {
        //获取所有的商品
        ArrayList<Goods> goodsList = GoodServices.goodsList;
        for (int i = 0; i < goodsList.size(); i++) {
            if (goodsList.get(i).getName().equals(goodsName)) {
                //在这进行购买
                Goods goods = goodsList.get(i);
                if (goods.getStock() < number) {
                    System.out.println("库存不足");
                    return;
                } else {
                    //进行购买
                    goods.setStock(goods.getStock() - number);
                    System.out.println("购买成功");
                }
            }
        }
    }

}
